import { getBaseUrl } from '@/services/urlconst';
import { getLevelFirst, getLevelZero, getUserID, ntkoPageUrlParams } from '@/utils';

/** 文件编制Manage数据范围权限判断 */
export const DocumentationManageTab = (curRes: Record<string, any>) => {
  if (curRes?.['/purchaseSourceController/ownCompanyID']) {
    return {
      ownCompanyID: getLevelZero(),
      params: 'ownCompanyID',
    };
  } else if (curRes?.['/purchaseSourceController/ownDepartmentID']) {
    return {
      ownDepartmentID: getLevelFirst(),
      params: 'ownDepartmentID',
    };
  } else if (curRes?.['/purchaseSourceController/ownerID']) {
    return {
      ownerID: getUserID(),
      params: 'ownerID',
    };
  }
};

/** 合同范本数据范围 */
export const contractManageSwitch = (curRes: Record<string, any>) => {
  if (curRes?.['/purchaseSourceController/CompanyID']) {
    return {
      ownCompanyID: getLevelZero(),
      params: 'ownCompanyID',
    };
  } else if (curRes?.['/purchaseSourceController/DepartmentID']) {
    return {
      ownDepartmentID: getLevelFirst(),
      params: 'ownDepartmentID',
    };
  }
  return {};
};

/** 在线编辑权限路径处理 */
const ntkoQueryParams = (resObj: Record<string, any>, curRes: Record<string, any>, fieldName: string) => {
  let wordInfo: Record<string, any> = {
    filedUrl: resObj?.[fieldName],
    ntkoWordUrl: '',
  };
  let wordBtnQuery: Record<string, any> = {
    wordAllBtn: false, //全部按钮
    saveBtn: false, //保存按钮
    replaceBtn: false, //替换域按钮
    needBtn: false, //需求填充按钮
    contractBtn: false, //合同填充
    evalBtn: false, //评分标准
  };
  if (curRes?.['/purchaseSourceController/saveToURL']) {
    wordBtnQuery['wordAllBtn'] = true;
    wordBtnQuery['saveBtn'] = true;
    wordBtnQuery['replaceBtn'] = true;
    /** 插入采购需求 */
    if (resObj?.needFileURL) {
      wordBtnQuery['needBtn'] = true;
    }
    /** 插入合同 */
    if (resObj?.contractURL) {
      wordBtnQuery['contractBtn'] = true;
    }
    /** 插入评分标准 */
    if (resObj?.evalFileURL) {
      wordBtnQuery['evalBtn'] = true;
    }
  }

  const wordQuery: string = ntkoPageUrlParams(wordBtnQuery);
  /** NTKO插件注释放开 */
  let ntkoWordUrl = `${getBaseUrl()}/static/NtkoPlugin/index.html?cmd=1&isReadOnly=${wordQuery}&filedUrl=${getBaseUrl()}${wordInfo?.filedUrl}`;
  // let ntkoWordUrl = `http://222.186.101.211:20103/static/NtkoPlugin/index.html?cmd=1&isReadOnly=${wordQuery}&filedUrl=http://222.186.101.211:20103${wordInfo?.filedUrl}`;
  // let ntkoWordUrl = `http://127.0.0.1:5500/index.html?cmd=1&isReadOnly=${wordQuery}&filedUrl=http://222.186.101.211:20103${wordInfo?.filedUrl}`;
  wordInfo['ntkoWordUrl'] = ntkoWordUrl;

  return {
    ...wordInfo,
    ntkoWordUrl: wordInfo?.ntkoWordUrl,
    filedUrl: wordInfo?.filedUrl,
    btnQuery: wordInfo,
  };
};

export { ntkoQueryParams };
